package com.example.system01.controller;

import com.example.system01.entity.Devices;
import com.example.system01.service.DevicesService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/devices")
//@RequestMapping("/devices")
public class DevicesController {
    //    实现与前端交互的数据请求方法
//    1、进入模块首页的时候需要对数据库里的所有商品
//    数据进行查询，返回列表给前端页面进行显示
    @Autowired
    DevicesService devicesService;
    @Autowired
    HttpServletRequest request;
    /***
     * 进入商品管理首页
     * @param model
     * @return
     */
    @RequestMapping("/devicesList")
//    @RequestMapping("/devicesList")
//    public String DevicesMain(Model model)
    public String DevicesMain(Model model){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
            model.addAttribute("username",username);
            List<Devices> devicesList = devicesService.findAll();
//        如何传递到前端
            model.addAttribute("data",devicesList);
//        返回到goods窗体页面
            System.out.println(devicesList);
            return "devices/devicesList";
        }
//       查询数据库里的数据
    }
    /***
     * 进入添加商品页面
     * @return
     */
    @RequestMapping("/devicesAdd")
    public String devicesAdd(Model model){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
            model.addAttribute("username",username);
            return "devices/devicesAdd";
        }

    }
    /**
     * 处理商品添加的方法
     * @param name
     * @param price
     * @param quantity
     * @param category
     * @param supplier
     * @return
     */
    @RequestMapping("/devicesAddProcess")
    public String devicesAddProcess(@RequestParam String name, @RequestParam String price
            , @RequestParam String quantity, @RequestParam String category, @RequestParam String supplier){

        //        组装一个goods实例
        Devices devices = new Devices();
        devices.setName(name);
        devices.setPrice(Double.valueOf(price));
        devices.setQuantity(Integer.valueOf(quantity));
        devices.setCategory(category);
        devices.setSupplier(supplier);
//        System.out.println(goods);
//        调用service的add方法给数据库
        if(devicesService.add(devices)==1){
            //        如果返回ok，那么就进入商品管理首页
            return "redirect:/devices/devicesList";
        }
        else{
            return "devices/devicesList";
        }
    }
    /**
     * 更新某条商品的记录
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/devicesUpdate")
    public String update(Model model,@RequestParam String id){
        Object username = request.getSession().getAttribute("username");
        if(username==null){
            return "redirect:/";
        }else{
            Devices devices=new Devices();
            devices.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
            Devices devicesDB = devicesService.findOne(devices);
//        2.将这些数据信息返回给视图
            model.addAttribute("username",username);
            model.addAttribute("data",devicesDB);
//        3.返回视图文件
            return "devices/devicesUpdate";
        }
//        首先基于id查询出来该id记录里的所有数据信息

    }
//    @RequestMapping("/devicesUpdateProcess")
//    public String devicesUpdateProcess(@RequestParam String name,@RequestParam String price
//            ,@RequestParam String quantity,@RequestParam String category,@RequestParam String supplier){
////        组装一个goods实例
//        Devices devices = new Devices();
//        devices.setName(name);
//        devices.setPrice(Double.valueOf(price));
//        devices.setQuantity(Integer.valueOf(quantity));
//        devices.setCategory(category);
//        devices.setSupplier(supplier);
//        System.out.println(devices);
////        调用service的add方法给数据库
//        if (devicesService.add(devices)==1){
//            //        如果返回ok，那么就进入商品管理首页
//            return "devices";
//        }
//        else{
//            return "devices";
//        }
//    }
}
